//#define __DEBUG__
// by Xeno
#define THIS_FILE "fn_showsidemain_d.sqf"
#include "x_setup.sqf"

#include "x_uiinc.sqf"

if (!X_Client) exitWith {};

disableSerialization;

PARAMS_1(_which);

if (_which == 1 && {__XJIPGetVar(GVAR(current_target_index)) == -1}) exitWith {};
if (_which == 0 && {(__XJIPGetVar(all_sm_res) || {__XJIPGetVar(GVAR(cur_sm_idx)) == -1})}) exitWith {};

_display = __uiGetVar(X_STATUS_DIALOG);

_ctrlmap = _display displayCtrl 11010;
ctrlMapAnimClear _ctrlmap;

#ifndef __TT__
_start_pos = position GVAR(FLAG_BASE);
#else
_start_pos = if (GVAR(player_side) == blufor) then {position GVAR(WFLAG_BASE)} else {position GVAR(EFLAG_BASE)};
#endif
_end_pos = [];
_exit_it = false;

_markername = "";
switch (_which) do {
	case 0: {
		_markername = format ["XMISSIONM%1", __XJIPGetVar(GVAR(cur_sm_idx)) + 1];
		_end_pos = markerPos _markername;
		if (str(_end_pos) == "[0,0,0]") then {_exit_it = true};
	};
	case 1: {
		_end_pos = markerPos QGVAR(dummy_marker);
		_markername = (GVAR(target_names) select __XJIPGetVar(GVAR(current_target_index))) select 1;
	};
};

if (_exit_it) exitWith {};

_dsmd = __pGetVar(GVAR(sidemain_m_do));
if (isNil "_dsmd") then {_dsmd = []};
if !(_markername in _dsmd) then {
	_dsmd set [count _dsmd, _markername];
	__pSetVar [QGVAR(sidemain_m_do), _dsmd];
	_markername spawn {
		scriptName "spawn_d_fnc_showsidemain_d_marker";
		private ["_m", "_a", "_aas"];
		_m = _this; _a = 1; _aas = -0.06;
		while {GVAR(showstatus_dialog_open) && {alive player} && {!__pGetVar(xr_pluncon)}} do {
			_m setMarkerAlphaLocal _a;
			_a = _a + _aas;
			if (_a < 0.3) then {_a = 0.3; _aas = _aas * -1};
			if (_a > 1.3) then {_a = 1.3; _aas = _aas * -1};
			sleep .1;
		};
		_m setMarkerAlphaLocal 1;
		__pSetVar [QGVAR(sidemain_m_do),[]];
	};
};

_ctrlmap ctrlmapanimadd [0.0, 1.00, _start_pos];
_ctrlmap ctrlmapanimadd [1.2, 1.00, _end_pos];
_ctrlmap ctrlmapanimadd [0.5, 0.30, _end_pos];
ctrlmapanimcommit _ctrlmap;